package com.godpaper.the3directs.busniess.managers
{
	import com.godpaper.the3directs.model.BoardModel;
	import com.godpaper.the3directs.model.PiecesModel;
	import com.godpaper.the3directs.model.PlayerModel;
	import com.godpaper.the3directs.util.PositionTransactUtil;
	
	import de.polygonal.ds.GraphNode;
	
	import mx.collections.ArrayCollection;

	public class BoardManager
	{
		public function BoardManager()
		{
		}
		
		
		public static function repopulation():void
		{
			//reset all pieces and gaskets
			PiecesModel.getInstance().selectedPiece.selected = false;
			PiecesModel.getInstance().selectedPiece = null;
		}
		
		//whether game ended and some pieces beKilled.
		public static function analysisGamePhases(reds:ArrayCollection,blues:ArrayCollection):Boolean
		{
			var adjacentContained:int = 0;
			var oppositeContained:int = 0;
			var hypotenuseContained:int = 0;
			trace("#####################analysisGamePhases begin##########################");
			if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
			{
				if(blues.length>=15)//why 15? It is just got right-angle's height/2*3 = 12 ,then +3;
				{
					for(var b:int=0;b<blues.length;b++)
					{
						if(PiecesModel.getInstance().adjacentCollection.contains(blues.getItemAt(b)))
						{
							adjacentContained++;
							PiecesModel.getInstance().adjacentCollectionBlue.addItem(blues.getItemAt(b));
						}
						if(PiecesModel.getInstance().oppositeCollection.contains(blues.getItemAt(b)))
						{
							oppositeContained++;
							PiecesModel.getInstance().oppositeCollectionBlue.addItem(blues.getItemAt(b));
						}
						if(PiecesModel.getInstance().hypotenuseCollection.contains(blues.getItemAt(b)))
						{
							hypotenuseContained++;
							PiecesModel.getInstance().hypotenuseCollectionBlue.addItem(blues.getItemAt(b));
						}
//						BoardModel.getInstance().gameGraphBlue.depthFirst( BoardModel.getInstance().gameGraphBlue.nodes[blueNodeIndex],depthFirstVisitedFunc);
					}
					if( adjacentContained>=1 && oppositeContained>=1 && hypotenuseContained>=1 )
					{
//						mx.controls.Alert.show("!!!Three BluePieces in 3 directs!!!");
						trace("!Three BluePieces in 3 directs!","adjacentContained:",adjacentContained,"oppositeContained:",oppositeContained,"hypotenuseContained:",hypotenuseContained);
						
						return PositionTransactUtil.doesThisThreeIsLinkable( PiecesModel.getInstance().adjacentCollectionBlue,
																			 PiecesModel.getInstance().oppositeCollectionBlue,
																			 PiecesModel.getInstance().hypotenuseCollectionBlue,
																			 BoardModel.getInstance().gameGraphBlue );
					}
				}
			}else
			{
				if(reds.length>=15)
				{
					for(var r:int=0;r<reds.length;r++)
					{
						if(PiecesModel.getInstance().adjacentCollection.contains(reds.getItemAt(r)))
						{
							adjacentContained++;
							PiecesModel.getInstance().adjacentCollectionRed.addItem(blues.getItemAt(r));
						}
						if(PiecesModel.getInstance().oppositeCollection.contains(reds.getItemAt(r)))
						{
							oppositeContained++;
							PiecesModel.getInstance().oppositeCollectionRed.addItem(blues.getItemAt(r));
						}
						if(PiecesModel.getInstance().hypotenuseCollection.contains(reds.getItemAt(r)))
						{
							hypotenuseContained++;
							PiecesModel.getInstance().hypotenuseCollectionRed.addItem(blues.getItemAt(r));
						}
					}
					if( adjacentContained>=1 && oppositeContained>=1 && hypotenuseContained>=1 )
					{
						trace("!Three RedPieces in 3 directs!","adjacentContained:",adjacentContained,"oppositeContained:",oppositeContained,"hypotenuseContained:",hypotenuseContained);
						return PositionTransactUtil.doesThisThreeIsLinkable( PiecesModel.getInstance().adjacentCollectionRed,
																			 PiecesModel.getInstance().oppositeCollectionRed,
																			 PiecesModel.getInstance().hypotenuseCollectionRed,
																			 BoardModel.getInstance().gameGraphRed );
					}
				}
			}
			
			trace("#####################analysisGamePhases end############################");
//			return (adjacentContained+oppositeContained+hypotenuseContained)>=3;
			return false;
		}
		//depth-first
		private static var depthFirstVisitedFunc:Function = function(node:GraphNode):Boolean
		{
			return true;
		}
	}
}